package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.readers.parameter.ModelAttributeParameterExpander;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    private static final String CART = "Cart_";

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void add(String skuId, Integer num, String username) {

        //添加前,已经登录了.
        //获取redis中,购物车数据
        //redis的hash结构
        //  大key 指定的是 Cart_用户名
        //  value(map集合,小key skuId value是orderItem) 购物车数据
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART+username).get(skuId);

        //当前商品已存在,直接更新数量和金额即可.
        if(orderItem != null){
            //设置最新数量, +1 或 -1
            orderItem.setNum(orderItem.getNum() + num);//如果数量为0,删除该购物车中的商品
            if(orderItem.getNum() <= 0){
                //删除redis中的购物车商品
                redisTemplate.boundHashOps(CART+username).delete(skuId);
                return;
            }
            //设置最新金额,总金额和支付金额
            orderItem.setMoney( orderItem.getNum() * orderItem.getPrice() );
            orderItem.setPayMoney( orderItem.getNum() * orderItem.getPrice() );
        }else {
            //当前商品不存在,封装购物车对象
            //从数据库中查询出数据,进行封装,spu和sku
            Result<Sku> skuResult = skuFeign.findById(skuId);
            Sku sku = skuResult.getData();
            Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());
            Spu spu = spuResult.getData();
            //进行购物车数据的封装,OrderItem
            orderItem = addGoodsToOrderItem(spu,sku,num);
        }

        //将新的购物车数据,存入到redis中
        redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);
    }

    @Override
    public Map<String, Object> list(String username) {
        Map<String,Object> map = new HashMap();
        //从redis中查询购物车列表
        //hash结构:
        //大Key  CAR_itcast
        //value  map集合  key skuId   value OrderItem
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        map.put("orderItemList",orderItemList);

        //商品数量和总价格的计算
        Integer totalNum = 0;
        Integer totalPrice = 0;
        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalPrice += orderItem.getMoney();
        }

        //封装
        map.put("totalNum",totalNum);
        map.put("totalPrice",totalPrice);

        return map;
    }

    private OrderItem addGoodsToOrderItem(Spu spu, Sku sku, Integer num) {
        //进行购物车数据的封装
        OrderItem orderItem = new OrderItem();

        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());

        orderItem.setNum(num);
        orderItem.setPrice(sku.getPrice());
        orderItem.setMoney( orderItem.getPrice() * num );
        orderItem.setPayMoney( orderItem.getPrice() * num );

        orderItem.setImage(sku.getImage());
        orderItem.setName(sku.getName());
        orderItem.setSkuId(sku.getId());
        orderItem.setSpuId(spu.getId());
        orderItem.setWeight( sku.getWeight() * num );

        return orderItem;
    }

}
